package com.dhy.service;

import java.sql.SQLException;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.zc.utils.JDBCUtils;

import dao.DBPool;
import entity.Hotel;
import entity.Payment;
import entity.Price;


public class AlipayService {

	
	//dao中的数据库连接对象
		private DBPool dbPool = DBPool.getInstance();
		private QueryRunner runner = new QueryRunner(dbPool.getDataSource());
		JDBCUtils jdbcUtils = new JDBCUtils();
		
		
		
		
		//增加商品信息
		public int addPayment(Payment payment) {
			try {
				String sql = "insert into payment values(null,?,?,?,?,?,?,?,?)";
				Object[] params = {payment.getNumber(),payment.getPrice(),payment.getTxt(),payment.getHotel_id(),payment.getCreate_time(),payment.getStatus(),payment.getPerson_id(),payment.getUser_id()};
				return runner.update(sql, params);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return 0;
		}
		
		
		
		
		//查询信息
		public Payment queryPayment(int price_id) {
			try {
				String sql = "select price.person_id,price.sum_price,hotel.hotel_text,price.hotel_id,price.person_id "
						+ "from price "
						+ "JOIN hotel "
						+ "on price.hotel_id = hotel.hotel_id "
						+ "where price_id = ?";
				Object[] params = {price_id};
				
				Payment payment = runner.query(sql, new BeanHandler<Payment>(Payment.class), params);
				System.out.println("queryPayment---->"+payment);
				return payment;
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		//取到最后一次存储的商品id
		//自己编写JDBC的方式
		//增加完之后，自动获取增加的自增主键值
		public Long getPriceIdJDBC(Payment payment) {
			try {
				String sql = "insert into price values(null,?,?,?,?)";
				Object[] params = {};
				return jdbcUtils.addObject(sql, params);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		public Price getPrice() {
			try {
				//long id = this.getPriceId();
				String sql = "select * from price where price_id = ?";
				Object[] params = {1};
				return runner.query(sql, new BeanHandler<Price>(Price.class), params);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return null;
			
		}
		
		/**
		 * 查询最后一条数据
		 * @param payment
		 * @return
		 */
		public Payment queryPayment() {
			String sql = "select * from payment where payment_id = (select max(payment_id) from payment);";
	    	Object[] params = {};
			try {
				return runner.query(sql, new BeanHandler<Payment>(Payment.class),params);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		}




		public Price queryPrice(int price_id) {
			String sql = "select * from price where price_id = ?";
			try {
			 return runner.query(sql, new BeanHandler<Price>(Price.class), price_id);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		}


		public Hotel querHotel(int hotel_id) {
			String sql = "select * from hotel where hotel_id = ?";
			try {
			 return runner.query(sql, new BeanHandler<Hotel>(Hotel.class), hotel_id);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		
		
		
		
		
//		public static void main(String[] args) {
//			Payment payment = new Payment();
//			payment.setPrice("6500");
//			payment.setTxt("哈哈哈");
//	        new AlipayService().addPayment(payment, "101020301105");
//	        System.out.println("ok了"); 
//		}
}
